<?php
/**
 * 地图选点
 * @author yupoxiong<i@yufuping.com>
 */

namespace generate\field;

use think\Exception;

class Map extends Field
{
    public static $html = <<<EOF

<div class="layui-form-item">
<div id="map[FIELD_NAME]" style="width: 100%; height: 350px;position: relative; background-color: rgb(229, 227, 223);overflow: hidden;transform: translateZ(0px);">
    <div id="tip">
        <input id="[FIELD_NAME]input" placeholder="请输入[FORM_NAME]" type="text"   onfocus='this.value=""'>
    </div>
</div>
</div>
<div class="layui-form-item">
<label for="[FIELD_NAME]" class="layui-form-label">[FORM_NAME]</label>
<div class="layui-input-inline">
<input name="[FIELD_NAME]" type="text"  class="layui-input" id="[FIELD_NAME]" value="{\$data.[FIELD_NAME]|default=''}">
</div>
</div>
<!--勿必把地图api key更换成自己的-->
<script type="text/javascript"
src='//webapi.amap.com/maps?v=1.4.2&key=cccf8ea926e153be0a013d55edd47c11'></script>
<!-- UI组件库 1.0 -->
<script src="//webapi.amap.com/ui/1.0/main.js?v=1.0.11"></script>

<script type="text/javascript">
var map = new AMap.Map("map[FIELD_NAME]", {
resizeEnable: true,
center: [117, 36.65],
zoom: 8,
keyboardEnable: false
});
var oldMarkerS = [];
AMap.plugin(['AMap.Autocomplete', 'AMap.PlaceSearch'], function () {
var autoOptions = {
    input: "[FIELD_NAME]input"
};
autocomplete = new AMap.Autocomplete(autoOptions);
var placeSearch = new AMap.PlaceSearch({
    map: map
})
AMap.event.addListener(autocomplete, "select", function (e) {
    placeSearch.setCity(e.poi.adcode);
    placeSearch.search(e.poi.name)
});
AMap.event.addListener(map, 'click', function (event) {
    var marker = new AMap.Marker({
        position: new AMap.LngLat(event.lnglat.lng, event.lnglat.lat),
    });
    map.remove(oldMarkerS)
    marker.setMap(map);
    oldMarkerS.push(marker);
    $('#longitude').val(event.lnglat.lng + ',' + event.lnglat.lat);
});

{if isset(\$data) && !empty(\$data['[FIELD_NAME]'])}
var marker = new AMap.Marker({
        position:  new AMap.LngLat({\$data.[FIELD_NAME]_arr.0},{\$data.[FIELD_NAME]_arr.1}) ,
    });
    map.remove(oldMarkerS)
    marker.setMap(map);
    oldMarkerS.push(marker);
{/if}
});
</script>

\n
EOF;
public static $modelAttrCode = <<<EOF
//[FORM_NAME]获取器
protected function get[P_FIELD_NAME]ArrAttr(\$value,\$data)
{
    return explode(',',\$data['[FIELD_NAME]']) ;

}
\n
EOF;
    public static $rules = [
        'required' => '非空',
        'lng_lat'  => '经纬度',
        'regular'  => '自定义正则'
    ];

    public static function create($data)
    {

        return str_replace(array('[FORM_NAME]', '[FIELD_NAME]', '[FIELD_DEFAULT]'), array($data['form_name'], $data['field_name'], $data['field_default']), self::$html);
    }
}